#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
inherit CometCompo_evolution_PM_P_TK_canvas_basic PM_TK_CANVAS

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_TK_canvas_basic constructor {name descr args} {
 this inherited $name $descr

   this set_GDD_id GDD_CometCompo_evolution_PM_P_TK_canvas_basic

 eval "$objName configure $args"
 return $objName
}

#___________________________________________________________________________________________________________________________________________
Methodes_set_LC CometCompo_evolution_PM_P_TK_canvas_basic [P_L_methodes_set_CometCompo_evolution] {} {}
Methodes_get_LC CometCompo_evolution_PM_P_TK_canvas_basic [P_L_methodes_get_CometCompo_evolution] {$this(FC)}

#___________________________________________________________________________________________________________________________________________
Generate_PM_setters CometCompo_evolution_PM_P_TK_canvas_basic [P_L_methodes_set_CometCompo_evolution_COMET_RE]

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_TK_canvas_basic Serialize {prefix} {
 this append_to_serialization "  <CometCompo_evolution_PM_P_TK_canvas_basic Move_to=\"$this(x) $this(y)\" Translate=\"0 0\" />\n"
}

#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_TK_canvas_basic get_or_create_prims {C_canvas} {
# Define the handle
 set canvas [$C_canvas get_canvas]
 this set_canvas $canvas
 
 this Add_MetaData PRIM_STYLE_CLASS [list $objName "COMPO COMPONENT" \
                                    ]


 set L_inputs  [this get_L_inputs]
 set L_outputs [this get_L_outputs]

 set this(x) 100
 set this(y) 100
 
 set this(box)   [$canvas create rectangle 100 100 300 200 -fill grey -tags [list $objName ${objName}_DRAG ${objName}_BOX]]
 
 set this(title) [$canvas create text 100 100 -text [[this get_LC] get_name] -tags [list $objName ${objName}_TITLE ${objName}_DRAG]]
 
 set y $this(y); set dy 15
 set x $this(x); set dx 150
 foreach input $L_inputs {
   lassign $input name card
   $canvas create text $x $y -text "$name ($card)"  -anchor nw -justify left  -tags [list $objName ${objName}_DRAG ${objName}_INPUT ${objName}_INPUT_TEXT]
   $canvas move [$canvas create rectangle 0 0 12 12 -fill #FFF -tags [list $objName ${objName}_INPUT ${objName}_INPUT_RECTANGLE ${objName}_INPUT_RECTANGLE_$name]] [expr $x - 15] [expr $y]
   $canvas bind ${objName}_INPUT_RECTANGLE_$name <ButtonPress>   "$objName Mouse_pressed_on_port  in $name %x %y"
   $canvas bind ${objName}_INPUT_RECTANGLE_$name <ButtonRelease> "$objName Mouse_released_on_port in $name %x %y"
   incr y $dy
  }

 incr x $dx; set y $this(y)
 foreach output $L_outputs {
   lassign $output name card
   $canvas create text $x $y -text "($card) $name"  -anchor ne -justify right -tags [list $objName ${objName}_DRAG ${objName}_OUTPUT ${objName}_OUTPUT_TEXT]
   $canvas move [$canvas create rectangle 0 0 12 12 -fill #FFF -tags [list $objName ${objName}_OUTPUT ${objName}_OUTPUT_RECTANGLE ${objName}_OUTPUT_RECTANGLE_$name]] [expr $x + 3] [expr $y]
   $canvas bind ${objName}_OUTPUT_RECTANGLE_$name <ButtonPress>   "$objName Mouse_pressed_on_port  out $name %x %y"
   $canvas bind ${objName}_OUTPUT_RECTANGLE_$name <ButtonRelease> "$objName Mouse_released_on_port out $name %x %y"
   incr y $dy
  }

# Move inputs and outputs with respect to theirs bounding boxes
 set bbox_in  [$canvas bbox ${objName}_INPUT ]
 set bbox_out [$canvas bbox ${objName}_OUTPUT]

 $canvas move ${objName}_OUTPUT [expr [lindex $bbox_in 2] + 30 - [lindex $bbox_out 0]] 0

 set bbox [$canvas bbox ${objName}_INPUT ${objName}_OUTPUT]
 $canvas coords ${objName}_TITLE  [expr ([lindex $bbox 0] + [lindex $bbox 2]) / 2] [expr [lindex $bbox 1] - 2*$dy]
 
 lassign [$canvas bbox ${objName}_INPUT_TEXT ${objName}_OUTPUT_TEXT ${objName}_TITLE] x1 y1 x2 y2
 $canvas coords $this(box) [expr $x1 - 10] [expr $y1 - 5] [expr $x2 + 10] [expr $y2 + 10]
 
# Moving the inputs and outputs near the borders
 set bbox     [$canvas bbox ${objName}]
 set bbox_in  [$canvas bbox ${objName}_INPUT ]
 set bbox_out [$canvas bbox ${objName}_OUTPUT]
 
 $canvas move ${objName}_INPUT  [expr [lindex $bbox 0] - [lindex $bbox_in  0] - 6] 0
 $canvas move ${objName}_OUTPUT [expr [lindex $bbox 2] - [lindex $bbox_out 2] + 6] 0

# Bindings for drag 
 $canvas bind ${objName}_DRAG <ButtonPress>   "if {%b == 1} {$C_canvas set_current_element $objName %x %y} else {$objName Display_drop_down_menu %x %y}"
 $canvas bind ${objName}_DRAG <ButtonRelease> "if {%b == 1} {$C_canvas set_current_element {} 0 0}"
									
									
 this set_root_for_daughters $C_canvas
 return [this set_prim_handle $C_canvas]
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_TK_canvas_basic get_coords_output_port {id} {
 set box [[this get_canvas] coords ${objName}_OUTPUT_RECTANGLE_$id]
 if {$box == ""} {return ""}
 
 lassign $box x1 y1 x2 y2
 
 return [list [expr $x2] [expr ($y1 + $y2)/2]]
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_TK_canvas_basic get_coords_input_port  {id} {
 set box [[this get_canvas] coords ${objName}_INPUT_RECTANGLE_$id]
 if {$box == ""} {return ""}
 
 lassign $box x1 y1 x2 y2
 
 return [list [expr $x1] [expr ($y1 + $y2)/2]]
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_TK_canvas_basic Mouse_pressed_on_port  {in_or_out port x y} {}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_TK_canvas_basic Mouse_released_on_port {in_or_out port x y} {}

#___________________________________________________________________________________________________________________________________________
Manage_CallbackList CometCompo_evolution_PM_P_TK_canvas_basic [list Mouse_pressed_on_port Mouse_released_on_port] begin

#___________________________________________________________________________________________________________________________________________
Inject_code CometCompo_evolution_PM_P_TK_canvas_basic set_can_process {} {
 set L_color [list #BBB #BFB]
 $this(canvas) itemconfigure ${objName}_BOX -fill [lindex $L_color $v]
}

#___________________________________________________________________________________________________________________________________________
Inject_code CometCompo_evolution_PM_P_TK_canvas_basic set_has_error {} {
 if {$v} {
   $this(canvas) itemconfigure ${objName}_BOX -fill #FBB
  }
}

#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_PM_P_TK_canvas_basic Display_drop_down_menu {x y} {
 set top ._toplevel_${objName}
 if {![winfo exists $top]} {
   lassign [winfo pointerxy $this(canvas)] x y

   toplevel $top; 
   set b $top.destroy
   button $b -text "Destroy component" -command "[this get_LC] dispose; destroy $top"
     pack $b -side top
   set pos 0
   foreach param [this get_L_param] {
     set f $top.f_$pos
	 frame $f; pack $f -side top -expand 1 -fill x
	 entry $f.ent -textvariable txt_var_${objName}_[lindex $param 0]
	   pack $f.ent -side right
	   global txt_var_${objName}_[lindex $param 0]
	   set txt_var_${objName}_[lindex $param 0] [lindex $param 1]
	 label $f.lab -text [lindex $param 0]; pack $f.lab -side right
	 
	 $f.ent configure -validate key -validatecommand "after 100 \"$objName prim_set_param_data {[lindex $param 0]} \\\[{$f.ent} get\\\]\"; return 1" 
     incr pos
	}
	
   set    cmd "lassign \[split \[wm geometry $top\] +\] dim tmp tmp; lassign \[split \$dim x\] tx ty; "
   append cmd "wm geometry $top \$dim+\[expr $x - \$tx / 2\]+$y"
   after 10 $cmd
  }
}


